package com.ruoyi.plan.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.Course.service.impl.StudyCourseServiceImpl;
import com.ruoyi.Goover.domain.StudyEnglish;
import com.ruoyi.Goover.service.IStudyEnglishService;
import com.ruoyi.Plancontent.domain.StudyPlancontent;
import com.ruoyi.Plancontent.service.IStudyPlancontentService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.plan.domain.StydyPlan;
import com.ruoyi.plan.service.IStydyPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 复习计划Controller
 * 
 * @author luobom
 * @date 2021-12-10
 */
@RestController
@RequestMapping("/plan/plan")
public class StydyPlanController extends BaseController
{
    @Autowired
    private IStydyPlanService stydyPlanService;
    @Autowired
    private IStudyPlancontentService studyPlancontentService;
    @Autowired
    private IStudyEnglishService studyEnglishService;
    @Autowired
    StudyCourseServiceImpl studyCourseService ;
    /**
     * 查询复习计划列表
     */
    @PreAuthorize("@ss.hasPermi('plan:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(StydyPlan stydyPlan)
    {
        //获取当前默认复习计划 如果没有则自动创建
        StudyPlancontent spc=new StudyPlancontent();
        spc.setMain("1");
        List<StudyPlancontent> Listspc=studyPlancontentService.selectStudyPlancontentList(spc);
        if(Listspc.size()==0){//创建默认复习计划
            spc.setContext("温故而知性～ 今日不复习，昨日白学习～");
            spc.setEmail("false");
            spc.setPlancontent("1.2.4.7.15.30.60.90.180");
            studyPlancontentService.insertStudyPlancontent(spc);
            Listspc.add(spc);
        }



        //获取需要执行的计划
        List<Map<String, Object>> res=stydyPlanService.selectplan("luo");

        startPage();
        stydyPlan.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        stydyPlan.setCourseuid(studyCourseService.GetDefaultCourse().getUid());
        List<StydyPlan> list = stydyPlanService.selectStydyPlanList(stydyPlan);
        //学习一天就有一天的复习计划 不等于就需要新增(删除复习计划就必须也删除当天的内容)

            //标记是否存在
            String bj="";
            for(Map<String,Object> j:res){
                for(StydyPlan s:list){
                    //去数据库查还有没有当前日期的单词，没有则删除任务
                    StudyEnglish syen=new StudyEnglish();
                    syen.setCreateTime(s.getPlantime());
                    syen.setCourseuid(studyCourseService.GetDefaultCourse().getUid());
                    List<StudyEnglish> listenglish = studyEnglishService.selectStudyEnglishList(syen);
                    if(listenglish.size()==0){
                        stydyPlanService.deleteStydyPlanByUid(s.getUid());
                        break;
                    }
                    //=============================================
                    if(String.valueOf(j.get("time")).equals(DateUtil.format(s.getPlantime(),DatePattern.NORM_DATE_PATTERN))){
                        //更新
                        s.setPlannumber(Long.valueOf(String.valueOf(j.get("count"))));
                        stydyPlanService.updateStydyPlan(s);
                        bj="yes";
                        break;
                    }
                }
                if(bj.equals("")){
                    String[] spt=Listspc.get(0).getPlancontent().split("\\.");
                    //增加计划
                    StydyPlan sp=new StydyPlan();
                    sp.setCourseuid(studyCourseService.GetDefaultCourse().getUid());
                    sp.setPlantime(DateUtil.parse(String.valueOf(j.get("time"))));
                    sp.setPlannumber(Long.valueOf(String.valueOf(j.get("count"))));
                    sp.setExist("未完成");
                    sp.setGoovereplan(Listspc.get(0).getPlancontent());
                    sp.setPlanstage(spt[0]);
                    sp.setStageovertime("");
                    sp.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                    stydyPlanService.insertStydyPlan(sp);
                }
                bj="";
            }



        list = stydyPlanService.selectStydyPlanList(stydyPlan);
        return getDataTable(list);
    }

    /**
     * 导出复习计划列表
     */
    @PreAuthorize("@ss.hasPermi('plan:plan:export')")
    @Log(title = "复习计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StydyPlan stydyPlan)
    {
        List<StydyPlan> list = stydyPlanService.selectStydyPlanList(stydyPlan);
        ExcelUtil<StydyPlan> util = new ExcelUtil<StydyPlan>(StydyPlan.class);
        util.exportExcel(response, list, "复习计划数据");
    }

    /**
     * 获取复习计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('plan:plan:query')")
    @GetMapping(value = "/{uid}")
    public AjaxResult getInfo(@PathVariable("uid") String uid)
    {
        return AjaxResult.success(stydyPlanService.selectStydyPlanByUid(uid));
    }

    /**
     * 新增复习计划
     */
    @PreAuthorize("@ss.hasPermi('plan:plan:add')")
    @Log(title = "复习计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StydyPlan stydyPlan)
    {
        stydyPlan.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(stydyPlanService.insertStydyPlan(stydyPlan));
    }

    /**
     * 修改复习计划
     */
    @PreAuthorize("@ss.hasPermi('plan:plan:edit')")
    @Log(title = "复习计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StydyPlan stydyPlan)
    {
        return toAjax(stydyPlanService.updateStydyPlan(stydyPlan));
    }

    /**
     * 删除复习计划
     */
    @PreAuthorize("@ss.hasPermi('plan:plan:remove')")
    @Log(title = "复习计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uids}")
    public AjaxResult remove(@PathVariable String[] uids)
    {
        return toAjax(stydyPlanService.deleteStydyPlanByUids(uids));
    }
}
